package com.sheep.opt;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: wnj
 * Date: 2010-11-2
 * Time: 17:31:29
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;

    public boolean isForceLogin(){
        return false;
    }

    public boolean isAuthorized(){
        return true;
    }

    @SuppressWarnings("unchecked")
    private static void addSessionTip(final String key,final String value){
        HttpSession hsession = ThreadUtil.currentHttpSession();
        ArrayList<String> list = (ArrayList<String>) hsession.getAttribute(key);
        if (list == null) {
            hsession.setAttribute(key, list = new ArrayList<String>());
        }
        list.add(value);
    }
    private static void removeSessionTip(final String key){
        HttpSession hsession = ThreadUtil.currentHttpSession();
        if (hsession!=null) {
            hsession.removeAttribute(key);
        }
    }

    public static void addMsg(final String msg) {
        addSessionTip("MSGS",msg);
    }
    public static void addInfo(final String info) {
        addSessionTip("INFOS",info);
    }
    public static void addErr(final String err) {
        addSessionTip("ERRS",err);
    }
    @SuppressWarnings("unchecked")
    public void addErr2Field(final String field, final String errMsg) {
        HttpSession hsession = ThreadUtil.currentHttpSession();
        if(null == hsession) {
            return;
        }
        Map<String, String> errs = (Map<String, String>) hsession.getAttribute("ERRFIELDS");
        if (errs == null) {
            errs = new HashMap<String, String>();
        }
        errs.put(field, errMsg);
        hsession.setAttribute("ERRFIELDS", errs);
    }

    public static void removeErrs() {
        removeSessionTip("ERRS");
    }
    public static void removeMsgs() {
        removeSessionTip("MSGS");
    }
    public static void removeInfos() {
        removeSessionTip("INFOS");
    }
    public static void removeErrFields() {
        HttpSession hsession = ThreadUtil.currentHttpSession();
        if (hsession!=null) {
            hsession.removeAttribute("ERRFIELDS");
        }
    }

    protected boolean isGetOrPostMethod() {
        String method  = request.getMethod();
        return ("get".equalsIgnoreCase(method) || "post".equalsIgnoreCase(method));
    }

    protected boolean isGetMethod(){
        String method  = request.getMethod();
        return ("get".equalsIgnoreCase(method));
    }

    protected boolean isPostMethod(){
        String method  = request.getMethod();
        return ("post".equalsIgnoreCase(method));
    }

    public void setServletResponse(javax.servlet.http.HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    public void setServletRequest(javax.servlet.http.HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }    
}
